<?php
class CustomersController extends GxController
{
    public function actionCreate()
    {
        $model = new Customers;
        if (!Yii::app()->request->isAjaxRequest)
            return;
        if (isset($_POST) && !empty($_POST)) {
            foreach ($_POST as $k => $v) {
                if (is_angka($v)) $v = get_number($v);
                $_POST['Customers'][$k] = $v;
            }
            $_POST['Customers']['awal'] = new CDbExpression('NOW()');
            $_POST['Customers']['akhir'] = new CDbExpression('NOW()');
            $model->attributes = $_POST['Customers'];
            $msg = t('save.fail', 'app');
            if ($model->save()) {
                $status = true;
                $msg = t('save.success.id', 'app', array('{id}' => $model->customer_id));
            } else {
                $msg .= " " . CHtml::errorSummary($model);
                $status = false;
            }
            echo CJSON::encode(array(
                'success' => $status,
                'msg' => $msg));
            Yii::app()->end();
        }
    }
    public function actionUpdate($id)
    {
        $model = $this->loadModel($id, 'Customers');
        if (isset($_POST) && !empty($_POST)) {
            foreach ($_POST as $k => $v) {
                if (is_angka($v)) $v = get_number($v);
                $_POST['Customers'][$k] = $v;
            }
            $msg = t('save.fail', 'app');
            $model->attributes = $_POST['Customers'];
            if ($model->save()) {
                $status = true;
                $msg = t('save.success.id', 'app', array('{id}' => $model->customer_id));
            } else {
                $msg .= " " . CHtml::errorSummary($model);
                $status = false;
            }
            if (Yii::app()->request->isAjaxRequest) {
                echo CJSON::encode(array(
                    'success' => $status,
                    'msg' => $msg
                ));
                Yii::app()->end();
            } else {
                $this->redirect(array('view', 'id' => $model->customer_id));
            }
        }
    }
    public function actionIndex()
    {
        if (isset($_POST['limit'])) {
            $limit = $_POST['limit'];
        } else {
            $limit = 20;
        }
        if (isset($_POST['start'])) {
            $start = $_POST['start'];
        } else {
            $start = 0;
        }
        $query = Yii::app()->db->createCommand();
        $query->select("nc.customer_id,nc.nama_customer,nc.no_customer,nc.tempat_lahir,nc.tgl_lahir,nc.email,
            nc.telp,nc.alamat,nc.awal,nc.akhir,nc.cabang_asal,nc.kecamatan_id,nk.kota_id,nko.provinsi_id,np.negara_id");
        $query->from = '{{customers}} AS nc';
        $query->join('{{kecamatan}} AS nk', '(nc.kecamatan_id = nk.kecamatan_id)');
        $query->join('{{kota}} AS nko', '(nk.kota_id = nko.kota_id)');
        $query->join('{{provinsi}} AS np', '(nko.provinsi_id = np.provinsi_id)');
//        $criteria = new CDbCriteria();
//        $param = array();
        if (isset($_POST['query'])) {
            $query->andWhere('nama_customer like :nama_customer', array(':nama_customer' => "%" . $_POST['query'] . "%"));
            $query->limit(25, 0);
        }
        if (isset($_POST['no_customer'])) {
//            $criteria->addCondition("no_customer like :no_customer");
//            $param[':no_customer'] = "%" . $_POST['no_customer'] . "%";
            $query->andWhere('no_customer like :no_customer', array(':no_customer' => "%" . $_POST['no_customer'] . "%"));
        }
        if (isset($_POST['nama_customer'])) {
//            $criteria->addCondition("nama_customer like :nama_customer");
//            $param[':nama_customer'] = "%" . $_POST['nama_customer'] . "%";
            $query->andWhere('nama_customer like :nama_customer', array(':nama_customer' => "%" . $_POST['nama_customer'] . "%"));
        }
        if (isset($_POST['telp'])) {
//            $criteria->addCondition("tempat_lahir like :tempat_lahir");
//            $param[':tempat_lahir'] = "%" . $_POST['tempat_lahir'] . "%";
            $query->andWhere('telp like :telp', array(':telp' => "%" . $_POST['telp'] . "%"));
        }
        if (isset($_POST['alamat'])) {
//            $criteria->addCondition("tgl_lahir = :tgl_lahir");
//            $param[':tgl_lahir'] = $_POST['tgl_lahir'];
            $query->andWhere('alamat like :alamat', array(':alamat' => "%" . $_POST['alamat'] . "%"));
        }
//        $criteria->params = $param;
        if ((isset ($_POST['mode']) && $_POST['mode'] == 'grid') ||
            (isset($_POST['limit']) && isset($_POST['start']))
        ) {
            $query->limit($limit, $start);
//            $criteria->limit = $limit;
//            $criteria->offset = $start;
        }
//        $model = Customers::model()->findAll($criteria);
//        $total = Customers::model()->count($criteria);
//        $this->renderJson($model, $total);
        $this->renderJsonArr($query->queryAll(true));
    }
}